package com.xqls.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import com.xqls.bean.RolePermissionKeyBean;
import com.xqls.biz.RoleBiz;
import com.xqls.biz.RolePermissionKeyBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/rolePermissionController")
public class RolePermissionController {
	
    @Autowired private RoleBiz roleBiz;

	@Autowired private RolePermissionKeyBiz rolePermissionBiz;

	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/rolePermission/rolePermissionList.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toPage")
	public ModelAndView toPage(){
		ModelAndView view=new ModelAndView("/rolePermission/rolePermissionList");
		view.addObject("roleList", this.roleBiz.getList());
		return view;
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/rolePermission/rolePermissionAdd.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toInsertPage")
	public ModelAndView toInsertPage(){
		ModelAndView view=new ModelAndView("/rolePermission/rolePermissionAdd");
		view.addObject("roleList", this.roleBiz.getList());
		return view;
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/rolePermission/rolePermissionSetUp.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toSetUpPage/{roleId}")
	public ModelAndView toEditPage(@PathVariable String roleId){
		ModelAndView view=new ModelAndView("/rolePermission/rolePermissionSetUp");
		view.addObject("roleBean", this.roleBiz.getById(roleId));
		return view;
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/rolePermission/rolePermissionListData.vm
	 * @param roleId
	 * @return ModelAndView
	 */
	@RequestMapping("/toTreePage/{roleId}")
	public ModelAndView toTreePage(@PathVariable String roleId){
		ModelAndView view=new ModelAndView("/rolePermission/rolePermissionListData");
		view.addObject("roleBean", this.roleBiz.getById(roleId));
		return view;
	}
	
	/**
	 * 根据角色编号、权限编号，设置角色权限
	 * @param roleId
	 * @param beanList
	 * @return JSON
	 */
	@RequestMapping("/setUpList/{roleId}")
	@ResponseBody
	public String setUpRecord(@PathVariable String roleId,@RequestBody List<RolePermissionKeyBean> beanList) {
		CommonResult commonResult = this.rolePermissionBiz.setUpRecord(roleId,beanList);
		for (int i=0;i<beanList.size();i++){
			System.out.println(beanList.get(i)+"---------");
		}
		return JSONObject.toJSONString(commonResult);
	}
	
	/**
	 * 分页显示角色权限信息
	 * @param commonPage
	 * @return CommonResult
	 */
	@RequestMapping("/getList")
	@ResponseBody
	public CommonResult getList(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = this.rolePermissionBiz.getListByPage(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}
}
